import { Controller, Get, Post, Body, Patch, Param, Delete, Inject, Query, HttpCode } from '@nestjs/common';
import { UserService } from './user.service';
import { RegisterUserDto } from './dto/register-user.dto';
import { Prisma } from '@prisma/client'
import { ResultData } from '@/common/util/result'
import { EmailService } from '../email/email.service';
import { RedisService } from '../redis/redis.service';
import { LoginUserDto } from './dto/login-user.dto';
import { UserId } from '@/common/decorator/param.decorator';
import { UpdatePasswordDto } from './dto/update-password.dto';
import { Prefix } from '@/common/enum';
import { UpdateUserDto } from './dto/update-user.dto';
import { ConfigService } from '@nestjs/config';
@Controller('user')
export class UserController {

    @Inject()
    private emailService: EmailService

    @Inject()
    private redisService: RedisService

    @Inject()
    private userService: UserService

    constructor(private configService: ConfigService) { }
    /**
     * @description: 注册验证码
     */
    @Get('captcha')
    async captcha(@Query('address') address: string) {
        if (this.configService.get('captcha') === 'svg') {
            return ResultData.ok(await this.userService.generateSvgCaptcha(Prefix.registerCaptcha))
        } else {
            let code = Math.random().toString().slice(2, 8)
            this.redisService.set(`${Prefix.registerCaptcha}${address}`, code, 60 * 5)
            await this.emailService.sendMail({
                to: address,
                subject: '聊天室',
                html: `用户注册：您的验证码是：${code}`
            })
            return ResultData.ok(null, '发送成功')
        }
    }
    /**
     * @description: 注册验证码
     */
    @Get('update/captcha')
    async updateCaptcha(@UserId() id: number) {
        const { email } = await this.userService.findUserById(id)
        let code = Math.random().toString().slice(2, 8)
        this.redisService.set(`${Prefix.updateCaptcha}${email}`, code, 60 * 5)
        await this.emailService.sendMail({
            to: email,
            subject: '聊天室',
            html: `修改密码：您的验证码是：${code}`
        })
        return ResultData.ok(null, '发送成功')
    }

    /**
     * @description: 注册用户
     */
    @Post('register')
    async register(@Body() registerUserDto: RegisterUserDto) {
        let data = await this.userService.register(registerUserDto)
        return ResultData.ok(data, '注册成功')
    }
    /**
     * @description: 用户登录
     */
    @Post('login')
    async login(@Body() loginUserDto: LoginUserDto) {
        return await this.userService.login(loginUserDto)
    }
    /**
     * @description: 用户退出登录
     */
    @Get('logout')
    async logout(@UserId() id: number) {
        return await this.userService.logout(id)
    }

    /**
     * @description: 获取用户信息
     */
    @Get('userInfo')
    async userInfo(@UserId() id: number) {
        return ResultData.ok(await this.userService.findUserById(id))
    }

    /**
     * @description: 修改用户信息
     */
    @Post('update')
    async updateUser(@UserId() id: number, @Body() updateUserDto: UpdateUserDto) {
        return await this.userService.update(id, updateUserDto)
    }
    /**
     * @description: 修改密码
     */
    @Post('updatePassword')
    async updatePassword(@UserId() id: number, @Body() passwordDto: UpdatePasswordDto) {
        return await this.userService.updatePassword(id, passwordDto)
    }

    @Get('test')
    async test(@Query() query: any) {
        console.log(query);
        return ResultData.ok(query)
    }
}
